// pages/detail/detail.js
var app = getApp()
var WxParse = require('../../wxParse/wxParse.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodId: 0,
    productDetail: {},
    number: 1,
    endTime: '00:00:00',
    kucun: false,
    buyBtn: true,
    coming: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      goodId: options.gid
    })
    this.initApp(options.gid)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * initApp
   */
  initApp: function(gid) {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/goods/detail?access_token=' + accessToken + '&id=' + gid,
        success: res => {
          if (res.data.status === 1) {
            WxParse.wxParse('content', 'html', res.data.data.content, that, 5)
            that.setData({
              productDetail: res.data.data
            })
            if (res.data.data.goods_number != 0) {
              that.endTime()
            } else {
              wx.hideLoading()
              that.setData({
                buyBtn: false
              })
            }
          }
          if (res.data.status === -10) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '请选择团长',
              success: res => {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../community/community',
                  })
                }
              }
            })
          }
          if (res.data.status === 0) {
            wx.hideLoading()
            wx.showModal({
              showCancel: false,
              confirmText: '返回首页',
              title: '提示',
              content: res.data.msg,
              success: res => {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../index/index',
                  })
                }
              }
            })
          }
        }
      })
    })
  },


  /**
   * 倒计时
   */
  endTime: function() {
    let that = this,
      timestamp = Date.parse(new Date()),
      endTime = that.data.productDetail.assemble_end_time,
      time = endTime - timestamp
    that.getLocalTime(time)
    setTimeout(function() {
      that.endTime()
    }, 1000)
  },

  /**
   * 时间戳转为为普通日期格式
   */
  getLocalTime: function(timestamp) {
    let that = this,
        now = Date.parse(new Date())
    // date = new Date(timestamp)
    // Y = date.getFullYear() + '-',
    // M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
    // D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ',
    // h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':',
    // m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':',
    // s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
    //------------------------------------------------------//

    let days = Math.floor(timestamp / (24 * 3600 * 1000)),
      dh = days * 24,
      lv1 = timestamp % (24 * 3600 * 1000),
      h = Math.floor(lv1 / (3600 * 1000)),
      lv2 = lv1 % (3600 * 1000),
      m = Math.floor(lv2 / (60 * 1000)),
      lv3 = lv2 % (60 * 1000),
      s = Math.floor(lv3 / 1000)
    if (that.data.productDetail.assemble_end_time * 1000 >= now && now >= that.data.productDetail.assemble_start_time * 1000) {
      that.setData({
        endTime: dh + h + ':' + m + ':' + s
      })
    } else if (that.data.productDetail.assemble_end_time > now) {
      that.setData({
        buyBtn: false
      })
    } else {
      that.setData({
        buyBtn: false
      })
    }
    wx.hideLoading()
  },

  /**
   * 确认订单信息
   */
  confirm: function() {
    let proList = [],
        goods = {},
        pro = this.data.productDetail
    goods.image = pro.goods_cover
    goods.goods_id = pro.goods_id
    goods.goods_name = pro.goods_name
    goods.price = pro.price
    goods.discount_price = pro.discount_price
    goods.number = this.data.number
    proList.push(goods)
    wx.navigateTo({
      url: '../confirm/confirm?goods=' + JSON.stringify(proList)
    })
  },

  /**
   * 添加购物车
   */
  addCart: function() {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/cart/add?access_token=' + accessToken,
        method: 'POST',
        data: {
          goods_id: that.data.goodId,
          num: 1
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.status === 1) {
            wx.hideLoading()
            wx.showToast({
              icon: 'none',
              title: '加入购物车成功',
              duration: 2000
            })
          } else {
            wx.hideLoading()
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
              duration: 2000
            })
          }
        }
      })
    })
  },

  /**
   * 返回首页
   */
  toHome: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  /**
   * 返回购物车
   */
  toCart: function() {
    wx.switchTab({
      url: '../cart/cart',
    })
  },

  /**
   * 分享
   */
  onShareAppMessage: function(e) {
    let that = this

    return {
      title: that.data.productDetail.goods_name,
    }
  },

  /**
   * 
   */
  endTime: function() {
    let that = this,
      timestamp = Date.parse(new Date()),
      endTime, time
    endTime = that.data.productDetail.assemble_end_time * 1000
    setTimeout(function() {
      time = endTime - timestamp
      that.getLocalTime(time)
      that.endTime()
    }, 1000)
  },
})